1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 package sun.print;
27
28 import java.awt.Dimension;
29 import java.awt.Frame;
30 import java.awt.Graphics;
31 import java.awt.Graphics2D;
32 import java.awt.PrintJob;
33 import java.awt.JobAttributes;
34 import java.awt.JobAttributes.*;
35 import java.awt.PageAttributes;
36 import java.awt.PageAttributes.*;
37
38 import java.awt.print.PageFormat;
39 import java.awt.print.Paper;
40 import java.awt.print.Printable;
41 import java.awt.print.PrinterException;
42 import java.awt.print.PrinterJob;
43
44 import java.io.File;
45 import java.io.FilePermission;
46 import java.io.IOException;
47
48 import java.net.URI;
49 import java.net.URISyntaxException;
50
51 import java.util.ArrayList;
52 import java.util.Properties;
53
54 import javax.print.PrintService;
55 import javax.print.attribute.HashPrintRequestAttributeSet;
56 import javax.print.attribute.PrintRequestAttributeSet;
57 import javax.print.attribute.ResolutionSyntax;
58 import javax.print.attribute.Size2DSyntax;
59 import javax.print.attribute.standard.Chromaticity;
60 import javax.print.attribute.standard.Copies;
61 import javax.print.attribute.standard.Destination;
62 import javax.print.attribute.standard.DialogTypeSelection;
63 import javax.print.attribute.standard.JobName;
64 import javax.print.attribute.standard.MediaSize;
65 import javax.print.attribute.standard.PrintQuality;
66 import javax.print.attribute.standard.PrinterResolution;
67 import javax.print.attribute.standard.SheetCollate;
68 import javax.print.attribute.standard.Sides;
69 import javax.print.attribute.standard.Media;
70 import javax.print.attribute.standard.OrientationRequested;
71 import javax.print.attribute.standard.MediaSizeName;
72 import javax.print.attribute.standard.PageRanges;
73
74 import sun.print.SunPageSelection;
75 import sun.print.SunMinMaxPage;
76
77
78
79
80
81
82
83
84 public class PrintJob2D extends PrintJob implements Printable, Runnable {
85
86 private static final MediaType SIZES[] = {
87 MediaType.ISO_4A0, MediaType.ISO_2A0, MediaType.ISO_A0,
88 MediaType.ISO_A1, MediaType.ISO_A2, MediaType.ISO_A3,
89 MediaType.ISO_A4, MediaType.ISO_A5, MediaType.ISO_A6,
90 MediaType.ISO_A7, MediaType.ISO_A8, MediaType.ISO_A9,
91 MediaType.ISO_A10, MediaType.ISO_B0, MediaType.ISO_B1,
92 MediaType.ISO_B2, MediaType.ISO_B3, MediaType.ISO_B4,
93 MediaType.ISO_B5, MediaType.ISO_B6, MediaType.ISO_B7,
94 MediaType.ISO_B8, MediaType.ISO_B9, MediaType.ISO_B10,
95 MediaType.JIS_B0, MediaType.JIS_B1, MediaType.JIS_B2,
96 MediaType.JIS_B3, MediaType.JIS_B4, MediaType.JIS_B5,
97 MediaType.JIS_B6, MediaType.JIS_B7, MediaType.JIS_B8,
98 MediaType.JIS_B9, MediaType.JIS_B10, MediaType.ISO_C0,
99 MediaType.ISO_C1, MediaType.ISO_C2, MediaType.ISO_C3,
100 MediaType.ISO_C4, MediaType.ISO_C5, MediaType.ISO_C6,
101 MediaType.ISO_C7, MediaType.ISO_C8, MediaType.ISO_C9,
102 MediaType.ISO_C10, MediaType.ISO_DESIGNATED_LONG,
103 MediaType.EXECUTIVE, MediaType.FOLIO, MediaType.INVOICE,
104 MediaType.LEDGER, MediaType.NA_LETTER, MediaType.NA_LEGAL,
105 MediaType.QUARTO, MediaType.A, MediaType.B,
106 MediaType.C, MediaType.D, MediaType.E,
107 MediaType.NA_10X15_ENVELOPE, MediaType.NA_10X14_ENVELOPE,
108 MediaType.NA_10X13_ENVELOPE, MediaType.NA_9X12_ENVELOPE,
109 MediaType.NA_9X11_ENVELOPE, MediaType.NA_7X9_ENVELOPE,
110 MediaType.NA_6X9_ENVELOPE, MediaType.NA_NUMBER_9_ENVELOPE,
111 MediaType.NA_NUMBER_10_ENVELOPE, MediaType.NA_NUMBER_11_ENVELOPE,
112 MediaType.NA_NUMBER_12_ENVELOPE, MediaType.NA_NUMBER_14_ENVELOPE,
113 MediaType.INVITE_ENVELOPE, MediaType.ITALY_ENVELOPE,
114 MediaType.MONARCH_ENVELOPE, MediaType.PERSONAL_ENVELOPE
115 };
116
117
118
119
120 private static final MediaSizeName JAVAXSIZES[] = {
121 null, null, MediaSizeName.ISO_A0,
122 MediaSizeName.ISO_A1, MediaSizeName.ISO_A2, MediaSizeName.ISO_A3,
123 MediaSizeName.ISO_A4, MediaSizeName.ISO_A5, MediaSizeName.ISO_A6,
124 MediaSizeName.ISO_A7, MediaSizeName.ISO_A8, MediaSizeName.ISO_A9,
125 MediaSizeName.ISO_A10, MediaSizeName.ISO_B0, MediaSizeName.ISO_B1,
126 MediaSizeName.ISO_B2, MediaSizeName.ISO_B3, MediaSizeName.ISO_B4,
127 MediaSizeName.ISO_B5, MediaSizeName.ISO_B6, MediaSizeName.ISO_B7,
128 MediaSizeName.ISO_B8, MediaSizeName.ISO_B9, MediaSizeName.ISO_B10,
129 MediaSizeName.JIS_B0, MediaSizeName.JIS_B1, MediaSizeName.JIS_B2,
130 MediaSizeName.JIS_B3, MediaSizeName.JIS_B4, MediaSizeName.JIS_B5,
131 MediaSizeName.JIS_B6, MediaSizeName.JIS_B7, MediaSizeName.JIS_B8,
132 MediaSizeName.JIS_B9, MediaSizeName.JIS_B10, MediaSizeName.ISO_C0,
133 MediaSizeName.ISO_C1, MediaSizeName.ISO_C2, MediaSizeName.ISO_C3,
134 MediaSizeName.ISO_C4, MediaSizeName.ISO_C5, MediaSizeName.ISO_C6,
135 null, null, null, null,
136 MediaSizeName.ISO_DESIGNATED_LONG, MediaSizeName.EXECUTIVE,
137 MediaSizeName.FOLIO, MediaSizeName.INVOICE, MediaSizeName.LEDGER,
138 MediaSizeName.NA_LETTER, MediaSizeName.NA_LEGAL,
139 MediaSizeName.QUARTO, MediaSizeName.A, MediaSizeName.B,
140 MediaSizeName.C, MediaSizeName.D, MediaSizeName.E,
141 MediaSizeName.NA_10X15_ENVELOPE, MediaSizeName.NA_10X14_ENVELOPE,
142 MediaSizeName.NA_10X13_ENVELOPE, MediaSizeName.NA_9X12_ENVELOPE,
143 MediaSizeName.NA_9X11_ENVELOPE, MediaSizeName.NA_7X9_ENVELOPE,
144 MediaSizeName.NA_6X9_ENVELOPE,
145 MediaSizeName.NA_NUMBER_9_ENVELOPE,
146 MediaSizeName.NA_NUMBER_10_ENVELOPE,
147 MediaSizeName.NA_NUMBER_11_ENVELOPE,
148 MediaSizeName.NA_NUMBER_12_ENVELOPE,
149 MediaSizeName.NA_NUMBER_14_ENVELOPE,
150 null, MediaSizeName.ITALY_ENVELOPE,
151 MediaSizeName.MONARCH_ENVELOPE, MediaSizeName.PERSONAL_ENVELOPE,
152 };
153
154
155
156 private static final int WIDTHS[] = {
157 4768, 3370, 2384, 1684,
158 1191, 842, 595, 420,
159 298, 210, 147, 105,
160 74, 2835, 2004, 1417,
161 1001, 709, 499, 354,
162 249, 176, 125, 88,
163 2920, 2064, 1460, 1032,
164 729, 516, 363, 258,
165 181, 128, 91, 2599,
166 1837, 1298, 918, 649,
167 459, 323, 230, 162,
168 113, 79, 312,
169 522, 612, 396, 792,
170 612, 612, 609, 612,
171 792, 1224, 1584, 2448,
172 720, 720,
173 720, 648,
174 648, 504,
175 432, 279,
176 297, 324,
177 342, 360,
178 624, 312,
179 279, 261
180 };
181 private static final int LENGTHS[] = {
182 6741, 4768, 3370, 2384,
183 1684, 1191, 842, 595,
184 420, 298, 210, 147,
185 105, 4008, 2835, 2004,
186 1417, 1001, 729, 499,
187 354, 249, 176, 125,
188 4127, 2920, 2064, 1460,
189 1032, 729, 516, 363,
190 258, 181, 128, 3677,
191 2599, 1837, 1298, 918,
192 649, 459, 323, 230,
193 162, 113, 624,
194 756, 936, 612, 1224,
195 792, 1008, 780, 792,
196 1224, 1584, 2448, 3168,
197 1080, 1008,
198 936, 864,
199 792, 648,
200 648, 639,
201 684, 747,
202 792, 828,
203 624, 652,
204 540, 468
205 };
206
207
208 private Frame frame;
209 private String docTitle = "";
210 private JobAttributes jobAttributes;
211 private PageAttributes pageAttributes;
212 private PrintRequestAttributeSet attributes;
213
214
215
216
217
218
219
220
221
222 private PrinterJob printerJob;
223
224
225
226
227 private PageFormat pageFormat;
228
229
230
231
232
233
234
235
236
237
238 private MessageQ graphicsToBeDrawn = new MessageQ("tobedrawn");
239
240
241
242
243
244
245
246
247
248 private MessageQ graphicsDrawn = new MessageQ("drawn");
249
250
251
252
253
254
255 private Graphics2D currentGraphics;
256
257
258
259
260
261 private int pageIndex = -1;
262
263
264
265 private final static String DEST_PROP = "awt.print.destination";
266 private final static String PRINTER = "printer";
267 private final static String FILE = "file";
268
269 private final static String PRINTER_PROP = "awt.print.printer";
270
271 private final static String FILENAME_PROP = "awt.print.fileName";
272
273 private final static String NUMCOPIES_PROP = "awt.print.numCopies";
274
275 private final static String OPTIONS_PROP = "awt.print.options";
276
277 private final static String ORIENT_PROP = "awt.print.orientation";
278 private final static String PORTRAIT = "portrait";
279 private final static String LANDSCAPE = "landscape";
280
281 private final static String PAPERSIZE_PROP = "awt.print.paperSize";
282 private final static String LETTER = "letter";
283 private final static String LEGAL = "legal";
284 private final static String EXECUTIVE = "executive";
285 private final static String A4 = "a4";
286
287 private Properties props;
288
289 private String options = "";
290
291
292
293
294
295 private Thread printerJobThread;
296
297 public PrintJob2D(Frame frame, String doctitle,
298 final Properties props) {
299 this.props = props;
300 this.jobAttributes = new JobAttributes();
301 this.pageAttributes = new PageAttributes();
302 translateInputProps();
303 initPrintJob2D(frame, doctitle,
304 this.jobAttributes, this.pageAttributes);
305 }
306
307 public PrintJob2D(Frame frame, String doctitle,
308 JobAttributes jobAttributes,
309 PageAttributes pageAttributes) {
310 initPrintJob2D(frame, doctitle, jobAttributes, pageAttributes);
311 }
312
313 private void initPrintJob2D(Frame frame, String doctitle,
314 JobAttributes jobAttributes,
315 PageAttributes pageAttributes) {
316
317 SecurityManager security = System.getSecurityManager();
318 if (security != null) {
319 security.checkPrintJobAccess();
320 }
321
322 if (frame == null &&
323 (jobAttributes == null ||
324 jobAttributes.getDialog() == DialogType.NATIVE)) {
325 throw new NullPointerException("Frame must not be null");
326 }
327 this.frame = frame;
328
329 this.docTitle = (doctitle == null) ? "" : doctitle;
330 this.jobAttributes = (jobAttributes != null)
331 ? jobAttributes : new JobAttributes();
332 this.pageAttributes = (pageAttributes != null)
333 ? pageAttributes : new PageAttributes();
334
335
336 int[][] pageRanges = this.jobAttributes.getPageRanges();
337 int first = pageRanges[0][0];
338 int last = pageRanges[pageRanges.length - 1][1];
339 this.jobAttributes.setPageRanges(new int[][] {
340 new int[] { first, last }
341 });
342 this.jobAttributes.setToPage(last);
343 this.jobAttributes.setFromPage(first);
344
345
346
347 int[] res = this.pageAttributes.getPrinterResolution();
348 if (res[0] != res[1]) {
349 throw new IllegalArgumentException("Differing cross feed and feed"+
350 " resolutions not supported.");
351 }
352
353
354 DestinationType dest= this.jobAttributes.getDestination();
355 if (dest == DestinationType.FILE) {
356 throwPrintToFile();
357
358
359 String destStr = jobAttributes.getFileName();
360 if ((destStr != null) &&
361 (jobAttributes.getDialog() == JobAttributes.DialogType.NONE)) {
362
363 File f = new File(destStr);
364 try {
365
366
367 if (f.createNewFile()) {
368 f.delete();
369 }
370 } catch (IOException ioe) {
371 throw new IllegalArgumentException("Cannot write to file:"+
372 destStr);
373 } catch (SecurityException se) {
374
375
376
377
378 }
379
380 File pFile = f.getParentFile();
381 if ((f.exists() &&
382 (!f.isFile() || !f.canWrite())) ||
383 ((pFile != null) &&
384 (!pFile.exists() || (pFile.exists() && !pFile.canWrite())))) {
385 throw new IllegalArgumentException("Cannot write to file:"+
386 destStr);
387 }
388 }
389 }
390 }
391
392 public boolean printDialog() {
393
394 boolean proceedWithPrint = false;
395
396 printerJob = PrinterJob.getPrinterJob();
397 if (printerJob == null) {
398 return false;
399 }
400 DialogType d = this.jobAttributes.getDialog();
401 PrintService pServ = printerJob.getPrintService();
402 if ((pServ == null) && (d == DialogType.NONE)){
403 return false;
404 }
405 copyAttributes(pServ);
406
407 DefaultSelectionType select =
408 this.jobAttributes.getDefaultSelection();
409 if (select == DefaultSelectionType.RANGE) {
410 attributes.add(SunPageSelection.RANGE);
411 } else if (select == DefaultSelectionType.SELECTION) {
412 attributes.add(SunPageSelection.SELECTION);
413 } else {
414 attributes.add(SunPageSelection.ALL);
415 }
416
417 if (frame != null) {
418 attributes.add(new DialogOwner(frame));
419 }
420
421 if ( d == DialogType.NONE) {
422 proceedWithPrint = true;
423 } else {
424 if (d == DialogType.NATIVE) {
425 attributes.add(DialogTypeSelection.NATIVE);
426 } else {
427 attributes.add(DialogTypeSelection.COMMON);
428 }
429 if (proceedWithPrint = printerJob.printDialog(attributes)) {
430 if (pServ == null) {
431
432
433
434 pServ = printerJob.getPrintService();
435 if (pServ == null) {
436 return false;
437 }
438 }
439 updateAttributes();
440 translateOutputProps();
441 }
442 }
443
444 if (proceedWithPrint) {
445
446 JobName jname = (JobName)attributes.get(JobName.class);
447 if (jname != null) {
448 printerJob.setJobName(jname.toString());
449 }
450
451 pageFormat = new PageFormat();
452
453 Media media = (Media)attributes.get(Media.class);
454 MediaSize mediaSize = null;
455 if (media != null && media instanceof MediaSizeName) {
456 mediaSize = MediaSize.getMediaSizeForName((MediaSizeName)media);
457 }
458
459 Paper p = pageFormat.getPaper();
460 if (mediaSize != null) {
461 p.setSize(mediaSize.getX(MediaSize.INCH)*72.0,
462 mediaSize.getY(MediaSize.INCH)*72.0);
463 }
464
465 if (pageAttributes.getOrigin()==OriginType.PRINTABLE) {
466
467 p.setImageableArea(18.0, 18.0,
468 p.getWidth()-36.0,
469 p.getHeight()-36.0);
470 } else {
471 p.setImageableArea(0.0,0.0,p.getWidth(),p.getHeight());
472 }
473
474 pageFormat.setPaper(p);
475
476 OrientationRequested orient =
477 (OrientationRequested)attributes.get(OrientationRequested.class);
478 if (orient!= null &&
479 orient == OrientationRequested.REVERSE_LANDSCAPE) {
480 pageFormat.setOrientation(PageFormat.REVERSE_LANDSCAPE);
481 } else if (orient == OrientationRequested.LANDSCAPE) {
482 pageFormat.setOrientation(PageFormat.LANDSCAPE);
483 } else {
484 pageFormat.setOrientation(PageFormat.PORTRAIT);
485 }
486
487 printerJob.setPrintable(this, pageFormat);
488
489 }
490
491 return proceedWithPrint;
492 }
493
494 private void updateAttributes() {
495 Copies c = (Copies)attributes.get(Copies.class);
496 jobAttributes.setCopies(c.getValue());
497
498 SunPageSelection sel =
499 (SunPageSelection)attributes.get(SunPageSelection.class);
500 if (sel == SunPageSelection.RANGE) {
501 jobAttributes.setDefaultSelection(DefaultSelectionType.RANGE);
502 } else if (sel == SunPageSelection.SELECTION) {
503 jobAttributes.setDefaultSelection(DefaultSelectionType.SELECTION);
504 } else {
505 jobAttributes.setDefaultSelection(DefaultSelectionType.ALL);
506 }
507
508 Destination dest = (Destination)attributes.get(Destination.class);
509 if (dest != null) {
510 jobAttributes.setDestination(DestinationType.FILE);
511 jobAttributes.setFileName(dest.getURI().getPath());
512 } else {
513 jobAttributes.setDestination(DestinationType.PRINTER);
514 }
515
516 PrintService serv = printerJob.getPrintService();
517 if (serv != null) {
518 jobAttributes.setPrinter(serv.getName());
519 }
520
521 PageRanges range = (PageRanges)attributes.get(PageRanges.class);
522 int[][] members = range.getMembers();
523 jobAttributes.setPageRanges(members);
524
525 SheetCollate collation =
526 (SheetCollate)attributes.get(SheetCollate.class);
527 if (collation == SheetCollate.COLLATED) {
528 jobAttributes.setMultipleDocumentHandling(
529 MultipleDocumentHandlingType.SEPARATE_DOCUMENTS_COLLATED_COPIES);
530 } else {
531 jobAttributes.setMultipleDocumentHandling(
532 MultipleDocumentHandlingType.SEPARATE_DOCUMENTS_UNCOLLATED_COPIES);
533 }
534
535 Sides sides = (Sides)attributes.get(Sides.class);
536 if (sides == Sides.TWO_SIDED_LONG_EDGE) {
537 jobAttributes.setSides(SidesType.TWO_SIDED_LONG_EDGE);
538 } else if (sides == Sides.TWO_SIDED_SHORT_EDGE) {
539 jobAttributes.setSides(SidesType.TWO_SIDED_SHORT_EDGE);
540 } else {
541 jobAttributes.setSides(SidesType.ONE_SIDED);
542 }
543
544
545
546 Chromaticity color =
547 (Chromaticity)attributes.get(Chromaticity.class);
548 if (color == Chromaticity.COLOR) {
549 pageAttributes.setColor(ColorType.COLOR);
550 } else {
551 pageAttributes.setColor(ColorType.MONOCHROME);
552 }
553
554 OrientationRequested orient =
555 (OrientationRequested)attributes.get(OrientationRequested.class);
556 if (orient == OrientationRequested.LANDSCAPE) {
557 pageAttributes.setOrientationRequested(
558 OrientationRequestedType.LANDSCAPE);
559 } else {
560 pageAttributes.setOrientationRequested(
561 OrientationRequestedType.PORTRAIT);
562 }
563
564 PrintQuality qual = (PrintQuality)attributes.get(PrintQuality.class);
565 if (qual == PrintQuality.DRAFT) {
566 pageAttributes.setPrintQuality(PrintQualityType.DRAFT);
567 } else if (qual == PrintQuality.HIGH) {
568 pageAttributes.setPrintQuality(PrintQualityType.HIGH);
569 } else {
570 pageAttributes.setPrintQuality(PrintQualityType.NORMAL);
571 }
572
573 Media msn = (Media)attributes.get(Media.class);
574 if (msn != null && msn instanceof MediaSizeName) {
575 MediaType mType = unMapMedia((MediaSizeName)msn);
576
577 if (mType != null) {
578 pageAttributes.setMedia(mType);
579 }
580 }
581 debugPrintAttributes(false, false);
582 }
583
584 private void debugPrintAttributes(boolean ja, boolean pa ) {
585 if (ja) {
586 System.out.println("new Attributes\ncopies = "+
587 jobAttributes.getCopies()+
588 "\nselection = "+
589 jobAttributes.getDefaultSelection()+
590 "\ndest "+jobAttributes.getDestination()+
591 "\nfile "+jobAttributes.getFileName()+
592 "\nfromPage "+jobAttributes.getFromPage()+
593 "\ntoPage "+jobAttributes.getToPage()+
594 "\ncollation "+
595 jobAttributes.getMultipleDocumentHandling()+
596 "\nPrinter "+jobAttributes.getPrinter()+
597 "\nSides2 "+jobAttributes.getSides()
598 );
599 }
600
601 if (pa) {
602 System.out.println("new Attributes\ncolor = "+
603 pageAttributes.getColor()+
604 "\norientation = "+
605 pageAttributes.getOrientationRequested()+
606 "\nquality "+pageAttributes.getPrintQuality()+
607 "\nMedia2 "+pageAttributes.getMedia()
608 );
609 }
610 }
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625 private void copyAttributes(PrintService printServ) {
626
627 attributes = new HashPrintRequestAttributeSet();
628 attributes.add(new JobName(docTitle, null));
629 PrintService pServ = printServ;
630
631 String printerName = jobAttributes.getPrinter();
632 if (printerName != null && printerName != ""
633 && !printerName.equals(pServ.getName())) {
634
635
636 PrintService []services = PrinterJob.lookupPrintServices();
637 try {
638 for (int i=0; i<services.length; i++) {
639 if (printerName.equals(services[i].getName())) {
640 printerJob.setPrintService(services[i]);
641 pServ = services[i];
642 break;
643 }
644 }
645 } catch (PrinterException pe) {
646 }
647 }
648
649 DestinationType dest = jobAttributes.getDestination();
650 if (dest == DestinationType.FILE &&
651 pServ.isAttributeCategorySupported(Destination.class)) {
652
653 String fileName = jobAttributes.getFileName();
654
655 Destination defaultDest;
656 if (fileName == null && (defaultDest = (Destination)pServ.
657 getDefaultAttributeValue(Destination.class)) != null) {
658 attributes.add(defaultDest);
659 } else {
660 URI uri = null;
661 try {
662 if (fileName != null) {
663 if (fileName.equals("")) {
664 fileName = ".";
665 }
666 } else {
667
668
669
670
671 fileName = "out.prn";
672 }
673 uri = (new File(fileName)).toURI();
674 } catch (SecurityException se) {
675 try {
676
677
678
679 fileName = fileName.replace('\\', '/');
680 uri = new URI("file:"+fileName);
681 } catch (URISyntaxException e) {
682 }
683 }
684 if (uri != null) {
685 attributes.add(new Destination(uri));
686 }
687 }
688 }
689 attributes.add(new SunMinMaxPage(jobAttributes.getMinPage(),
690 jobAttributes.getMaxPage()));
691 SidesType sType = jobAttributes.getSides();
692 if (sType == SidesType.TWO_SIDED_LONG_EDGE) {
693 attributes.add(Sides.TWO_SIDED_LONG_EDGE);
694 } else if (sType == SidesType.TWO_SIDED_SHORT_EDGE) {
695 attributes.add(Sides.TWO_SIDED_SHORT_EDGE);
696 } else if (sType == SidesType.ONE_SIDED) {
697 attributes.add(Sides.ONE_SIDED);
698 }
699
700 MultipleDocumentHandlingType hType =
701 jobAttributes.getMultipleDocumentHandling();
702 if (hType ==
703 MultipleDocumentHandlingType.SEPARATE_DOCUMENTS_COLLATED_COPIES) {
704 attributes.add(SheetCollate.COLLATED);
705 } else {
706 attributes.add(SheetCollate.UNCOLLATED);
707 }
708
709 attributes.add(new Copies(jobAttributes.getCopies()));
710
711 attributes.add(new PageRanges(jobAttributes.getFromPage(),
712 jobAttributes.getToPage()));
713
714 if (pageAttributes.getColor() == ColorType.COLOR) {
715 attributes.add(Chromaticity.COLOR);
716 } else {
717 attributes.add(Chromaticity.MONOCHROME);
718 }
719
720 pageFormat = printerJob.defaultPage();
721 if (pageAttributes.getOrientationRequested() ==
722 OrientationRequestedType.LANDSCAPE) {
723 pageFormat.setOrientation(PageFormat.LANDSCAPE);
724 attributes.add(OrientationRequested.LANDSCAPE);
725 } else {
726 pageFormat.setOrientation(PageFormat.PORTRAIT);
727 attributes.add(OrientationRequested.PORTRAIT);
728 }
729
730 MediaType media = pageAttributes.getMedia();
731 MediaSizeName msn = mapMedia(media);
732 if (msn != null) {
733 attributes.add(msn);
734 }
735
736 PrintQualityType qType =
737 pageAttributes.getPrintQuality();
738 if (qType == PrintQualityType.DRAFT) {
739 attributes.add(PrintQuality.DRAFT);
740 } else if (qType == PrintQualityType.NORMAL) {
741 attributes.add(PrintQuality.NORMAL);
742 } else if (qType == PrintQualityType.HIGH) {
743 attributes.add(PrintQuality.HIGH);
744 }
745 }
746
747
748
749
750
751
752
753
754 public Graphics getGraphics() {
755
756 Graphics printGraphics = null;
757
758 synchronized (this) {
759 ++pageIndex;
760
761
762
763
764 if (pageIndex == 0 && !graphicsToBeDrawn.isClosed()) {
765
766
767
768
769
770
771
772
773 startPrinterJobThread();
774
775 }
776 notify();
777 }
778
779
780
781
782
783
784 if (currentGraphics != null) {
785 graphicsDrawn.append(currentGraphics);
786 currentGraphics = null;
787 }
788
789
790
791
792
793 currentGraphics = graphicsToBeDrawn.pop();
794
795 if (currentGraphics instanceof PeekGraphics) {
796 ( (PeekGraphics) currentGraphics).setAWTDrawingOnly();
797 graphicsDrawn.append(currentGraphics);
798 currentGraphics = graphicsToBeDrawn.pop();
799 }
800
801
802 if (currentGraphics != null) {
803
804
805
806
807
808
809
810
811
812 currentGraphics.translate(pageFormat.getImageableX(),
813 pageFormat.getImageableY());
814
815
816 double awtScale = 72.0/getPageResolutionInternal();
817 currentGraphics.scale(awtScale, awtScale);
818
819
820
821
822
823
824
825
826
827 printGraphics = new ProxyPrintGraphics(currentGraphics.create(),
828 this);
829
830 }
831
832 return printGraphics;
833 }
834
835
836
837
838
839
840
841
842 public Dimension getPageDimension() {
843 double wid, hgt, scale;
844 if (pageAttributes != null &&
845 pageAttributes.getOrigin()==OriginType.PRINTABLE) {
846 wid = pageFormat.getImageableWidth();
847 hgt = pageFormat.getImageableHeight();
848 } else {
849 wid = pageFormat.getWidth();
850 hgt = pageFormat.getHeight();
851 }
852 scale = getPageResolutionInternal() / 72.0;
853 return new Dimension((int)(wid * scale), (int)(hgt * scale));
854 }
855
856 private double getPageResolutionInternal() {
857 if (pageAttributes != null) {
858 int []res = pageAttributes.getPrinterResolution();
859 if (res[2] == 3) {
860 return res[0];
861 } else {
862 return (res[0] * 2.54);
863 }
864 } else {
865 return 72.0;
866 }
867 }
868
869
870
871
872
873
874 public int getPageResolution() {
875 return (int)getPageResolutionInternal();
876 }
877
878
879
880
881 public boolean lastPageFirst() {
882 return false;
883 }
884
885
886
887
888 public synchronized void end() {
889
890
891
892
893 graphicsToBeDrawn.close();
894
895
896
897
898
899
900
901
902 if (currentGraphics != null) {
903 graphicsDrawn.append(currentGraphics);
904 }
905 graphicsDrawn.closeWhenEmpty();
906
907
908
909
910
911 if( printerJobThread != null && printerJobThread.isAlive() ){
912 try {
913 printerJobThread.join();
914 } catch (InterruptedException e) {
915 }
916 }
917 }
918
919
920
921
922
923 public void finalize() {
924 end();
925 }
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951 public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
952 throws PrinterException {
953
954 int result;
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970 graphicsToBeDrawn.append( (Graphics2D) graphics);
971
972
973
974
975
976
977
978
979 if (graphicsDrawn.pop() != null) {
980 result = PAGE_EXISTS;
981 } else {
982 result = NO_SUCH_PAGE;
983 }
984
985 return result;
986 }
987
988 private void startPrinterJobThread() {
989
990 printerJobThread = new Thread(this, "printerJobThread");
991 printerJobThread.start();
992 }
993
994
995 public void run() {
996
997 try {
998 printerJob.print(attributes);
999 } catch (PrinterException e) {
1000
1001 }
1002
1003
1004
1005
1006 graphicsToBeDrawn.closeWhenEmpty();
1007 graphicsDrawn.close();
1008 }
1009
1010 private class MessageQ {
1011
1012 private String qid="noname";
1013
1014 private ArrayList queue = new ArrayList();
1015
1016 MessageQ(String id) {
1017 qid = id;
1018 }
1019
1020 synchronized void closeWhenEmpty() {
1021
1022 while (queue != null && queue.size() > 0) {
1023 try {
1024 wait(1000);
1025 } catch (InterruptedException e) {
1026
1027 }
1028 }
1029
1030 queue = null;
1031 notifyAll();
1032 }
1033
1034 synchronized void close() {
1035 queue = null;
1036 notifyAll();
1037 }
1038
1039 synchronized boolean append(Graphics2D g) {
1040
1041 boolean queued = false;
1042
1043 if (queue != null) {
1044 queue.add(g);
1045 queued = true;
1046 notify();
1047 }
1048
1049 return queued;
1050 }
1051
1052 synchronized Graphics2D pop() {
1053 Graphics2D g = null;
1054
1055 while (g == null && queue != null) {
1056
1057 if (queue.size() > 0) {
1058 g = (Graphics2D) queue.remove(0);
1059 notify();
1060
1061 } else {
1062 try {
1063 wait(2000);
1064 } catch (InterruptedException e) {
1065
1066 }
1067 }
1068 }
1069
1070 return g;
1071 }
1072
1073 synchronized boolean isClosed() {
1074 return queue == null;
1075 }
1076
1077 }
1078
1079
1080 private static int[] getSize(MediaType mType) {
1081 int []dim = new int[2];
1082 dim[0] = 612;
1083 dim[1] = 792;
1084
1085 for (int i=0; i < SIZES.length; i++) {
1086 if (SIZES[i] == mType) {
1087 dim[0] = WIDTHS[i];
1088 dim[1] = LENGTHS[i];
1089 break;
1090 }
1091 }
1092 return dim;
1093 }
1094
1095 public static MediaSizeName mapMedia(MediaType mType) {
1096 MediaSizeName media = null;
1097
1098
1099
1100 int length = Math.min(SIZES.length, JAVAXSIZES.length);
1101
1102 for (int i=0; i < length; i++) {
1103 if (SIZES[i] == mType) {
1104 if ((JAVAXSIZES[i] != null) &&
1105 MediaSize.getMediaSizeForName(JAVAXSIZES[i]) != null) {
1106 media = JAVAXSIZES[i];
1107 break;
1108 } else {
1109
1110 media = new CustomMediaSizeName(SIZES[i].toString());
1111
1112 float w = (float)Math.rint(WIDTHS[i] / 72.0);
1113 float h = (float)Math.rint(LENGTHS[i] / 72.0);
1114 if (w > 0.0 && h > 0.0) {
1115
1116
1117 new MediaSize(w, h, Size2DSyntax.INCH, media);
1118 }
1119
1120 break;
1121 }
1122 }
1123 }
1124 return media;
1125 }
1126
1127
1128 public static MediaType unMapMedia(MediaSizeName mSize) {
1129 MediaType media = null;
1130
1131
1132
1133 int length = Math.min(SIZES.length, JAVAXSIZES.length);
1134
1135 for (int i=0; i < length; i++) {
1136 if (JAVAXSIZES[i] == mSize) {
1137 if (SIZES[i] != null) {
1138 media = SIZES[i];
1139 break;
1140 }
1141 }
1142 }
1143 return media;
1144 }
1145
1146 private void translateInputProps() {
1147 if (props == null) {
1148 return;
1149 }
1150
1151 String str;
1152
1153 str = props.getProperty(DEST_PROP);
1154 if (str != null) {
1155 if (str.equals(PRINTER)) {
1156 jobAttributes.setDestination(DestinationType.PRINTER);
1157 } else if (str.equals(FILE)) {
1158 jobAttributes.setDestination(DestinationType.FILE);
1159 }
1160 }
1161 str = props.getProperty(PRINTER_PROP);
1162 if (str != null) {
1163 jobAttributes.setPrinter(str);
1164 }
1165 str = props.getProperty(FILENAME_PROP);
1166 if (str != null) {
1167 jobAttributes.setFileName(str);
1168 }
1169 str = props.getProperty(NUMCOPIES_PROP);
1170 if (str != null) {
1171 jobAttributes.setCopies(Integer.parseInt(str));
1172 }
1173
1174 this.options = props.getProperty(OPTIONS_PROP, "");
1175
1176 str = props.getProperty(ORIENT_PROP);
1177 if (str != null) {
1178 if (str.equals(PORTRAIT)) {
1179 pageAttributes.setOrientationRequested(
1180 OrientationRequestedType.PORTRAIT);
1181 } else if (str.equals(LANDSCAPE)) {
1182 pageAttributes.setOrientationRequested(
1183 OrientationRequestedType.LANDSCAPE);
1184 }
1185 }
1186 str = props.getProperty(PAPERSIZE_PROP);
1187 if (str != null) {
1188 if (str.equals(LETTER)) {
1189 pageAttributes.setMedia(SIZES[MediaType.LETTER.hashCode()]);
1190 } else if (str.equals(LEGAL)) {
1191 pageAttributes.setMedia(SIZES[MediaType.LEGAL.hashCode()]);
1192 } else if (str.equals(EXECUTIVE)) {
1193 pageAttributes.setMedia(SIZES[MediaType.EXECUTIVE.hashCode()]);
1194 } else if (str.equals(A4)) {
1195 pageAttributes.setMedia(SIZES[MediaType.A4.hashCode()]);
1196 }
1197 }
1198 }
1199
1200 private void translateOutputProps() {
1201 if (props == null) {
1202 return;
1203 }
1204
1205 String str;
1206
1207 props.setProperty(DEST_PROP,
1208 (jobAttributes.getDestination() == DestinationType.PRINTER) ?
1209 PRINTER : FILE);
1210 str = jobAttributes.getPrinter();
1211 if (str != null && !str.equals("")) {
1212 props.setProperty(PRINTER_PROP, str);
1213 }
1214 str = jobAttributes.getFileName();
1215 if (str != null && !str.equals("")) {
1216 props.setProperty(FILENAME_PROP, str);
1217 }
1218 int copies = jobAttributes.getCopies();
1219 if (copies > 0) {
1220 props.setProperty(NUMCOPIES_PROP, "" + copies);
1221 }
1222 str = this.options;
1223 if (str != null && !str.equals("")) {
1224 props.setProperty(OPTIONS_PROP, str);
1225 }
1226 props.setProperty(ORIENT_PROP,
1227 (pageAttributes.getOrientationRequested() ==
1228 OrientationRequestedType.PORTRAIT)
1229 ? PORTRAIT : LANDSCAPE);
1230 MediaType media = SIZES[pageAttributes.getMedia().hashCode()];
1231 if (media == MediaType.LETTER) {
1232 str = LETTER;
1233 } else if (media == MediaType.LEGAL) {
1234 str = LEGAL;
1235 } else if (media == MediaType.EXECUTIVE) {
1236 str = EXECUTIVE;
1237 } else if (media == MediaType.A4) {
1238 str = A4;
1239 } else {
1240 str = media.toString();
1241 }
1242 props.setProperty(PAPERSIZE_PROP, str);
1243 }
1244
1245 private void throwPrintToFile() {
1246 SecurityManager security = System.getSecurityManager();
1247 FilePermission printToFilePermission = null;
1248 if (security != null) {
1249 if (printToFilePermission == null) {
1250 printToFilePermission =
1251 new FilePermission("<<ALL FILES>>", "read,write");
1252 }
1253 security.checkPermission(printToFilePermission);
1254 }
1255 }
1256
1257 }